#!/bin/sh

# $0 - shell file; $1 - dev/test/prod; $2 - hudson build

# --- setup ---

PROJECT_PATH=`cd \`dirname $0\`;cd ..;pwd`
TIMESTAMP=`date +%Y%m%d-%H%M`

JAVA_LIBS=.
for f in `ls ${PROJECT_PATH}/WebRoot/WEB-INF/lib/*.jar`;do
    JAVA_LIBS=${JAVA_LIBS}:${f}
done
for f in `ls ${PROJECT_PATH}/container-libs/*.jar`;do
    JAVA_LIBS=${JAVA_LIBS}:${f}
done

cd ${PROJECT_PATH}
rm -rf output
mkdir output
cd output

# --- copy and compile ---

cp -r ${PROJECT_PATH}/WebRoot .
mkdir ./WebRoot/WEB-INF/classes
#cp -r ${PROJECT_PATH}/petmain1.0/src/conf ./WebRoot/WEB-INF/classes/
cp -r ${PROJECT_PATH}/petmain1.0/src/config ./WebRoot/WEB-INF/classes/
#cp -r ${PROJECT_PATH}/petframework/java/config ./WebRoot/WEB-INF/classes/
#mv ./WebRoot/WEB-INF/classes/conf/${1}/* ./WebRoot/WEB-INF/classes/conf/
cp -f ${PROJECT_PATH}/petframework/java/config/struts/*.xml ./WebRoot/WEB-INF/classes/config/struts/
mv ./WebRoot/WEB-INF/classes/config/${1}/* ./WebRoot/WEB-INF/classes/config/

echo "version=${TIMESTAMP}.${2}.zip" >./WebRoot/WEB-INF/classes/config/version.properties

javac -encoding "utf-8" -cp ${JAVA_LIBS} -d ./WebRoot/WEB-INF/classes `find ${PROJECT_PATH}/pet* -name *.java`
echo "erp.${1}.${TIMESTAMP}.${2}.zip" >>./WebRoot/version.txt
rm -rf ./WebRoot/WEB-INF/classes/config/test
rm -rf ./WebRoot/WEB-INF/classes/config/prod
#rm -rf ./WebRoot/WEB-INF/classes/conf/test
#rm -rf ./WebRoot/WEB-INF/classes/conf/prod

# --- pack ---

cd WebRoot
zip -r ${PROJECT_PATH}/output/erp.${1}.${TIMESTAMP}.${2}.zip * 
